<?php
/**
 * @Author: Ron Chen
 * @Date:   2017-03-23 22:33:51
 * @Last Modified by:   Ron Chen
 * @Last Modified time: 2018-05-11 00:15:59
 */
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\AccountInfo;
use app\models\AccountWechat;


class BaseController extends Controller {

	public $title   = '';
	public $desc    = '';
	public $layout  = "insurance";
	public $request; 
	public $user_id; 
	
	public function init(){
		$this->layout = "insurance";
		$this->title  = "趣寻网";
		$this->desc   = "趣寻网-打造互联网保险小生态";
		$this->request = \Yii::$app->request;
		
		$this->check();
	}

	public function check(){
		$oauth = \Yii::$app->wechat->app->oauth;
		$session = \Yii::$app->session;
		// 未登录Wechat
		if (empty ( $session ['user_wechat'] )) {
			$session ['wechat_target_url'] = \Yii::$app->request->url;
			// $session ['wechat_target_url'] = 'auth.funfind.cn?redirect_uri='+\Yii::$app->request->url;
			$oauth->redirect ()->send ();
		}
		$userinfo = $session ['user_wechat'];
		$original_info = $userinfo["original"];
		$wechat_info = [
			"open_id" => $original_info["openid"],
			"wx_id"   => $userinfo["id"],
			"name"    => $userinfo["name"],
			"avatar"  => $userinfo["avatar"],
			"email"   => $userinfo["email"],
			"country"  => $original_info["country"],
			"province" => $original_info["province"],
			"city"     => $original_info["city"],
			"language" => $original_info["language"],
			"country"  => $original_info["country"],
		];
		$ff_count_id = AccountWechat::initRecord($wechat_info);
		$session['user_info'] = $ff_count_id;
		// 已经登录过Wechat
		$this->user_id = $session['user_info'];
	}

    /**
     * 请求第三方，并传递一个数组
     * @param $url
     * @param array $data
     */
    public function postResponseHandle($url, $data = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//用post方法传送参数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        curl_close($ch);
    }
}
	